var check_username = /^[\u4e00-\u9fa5]{1,9}$|^[\w]{1,18}$|^[\u4e00-\u9fa5\w]{1,9}$/;
var check_password = /^[^\u4E00-\u9FFF\s]{5,20}$/;
var check_email = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
var check_teleNum = /^[\d]{11}$/;

//手机号
var $teleNum = $("#teleNum");
var $teleP = $("#teleP");
var $teleDiv = $("#teleDiv");
var teleflag = false;
$teleNum.blur(function(){
	//每次进入函数都进行初始化为false
	teleflag = false;
	var value = $teleNum.val();
	if(value.length>0 && check_teleNum.test(value)){
		var result = checkContain("teleNum",value);
		if (result=="1") {//数据库不存在，通过
			$teleP.text("");
			$teleP.css("display","none");
			$teleDiv.css("margin-bottom","20px");
			teleflag = true;
		}else{//不通过
			$teleP.text("手机号码已存在!");
			$teleP.css("display","block");
			$teleDiv.css("margin-bottom","30px");
		}
		
	}else{
		$teleP.text("请输入有效手机号");
		$teleP.css("display","block");
		$teleDiv.css("margin-bottom","30px");
	}
})
//用户名
var $username = $("input[name=username]");
var $usernameP = $("#nameP");
var $userDiv = $("#nameDiv");
var userflag = false;
$username.blur(function(){
	//每次进入函数都进行初始化为false
	userflag = false;
	var value = $username.val();
	if(value.length>=4 && check_username.test(value)){
		var result = checkContain("username",value);
		if (result=="1") {//数据库不存在，通过
			$usernameP.text("");
			$usernameP.css("display","none");
			$userDiv.css("margin-bottom","20px");
			userflag = true;
		}else{//不通过
			$usernameP.text("名称已存在!");
			$usernameP.css("display","block");
			$userDiv.css("margin-bottom","30px");
		}
	}else{
		if(value.length<4){
			$usernameP.text("字符长度必须大于等于4，一个中文字算2个字符");
			$usernameP.css("display","block");
			$userDiv.css("margin-bottom","30px");
		}else if(value.length>18){
			$usernameP.text("字符长度必须小于等于18，一个中文字算2个字符");
			$usernameP.css("display","block");
			$userDiv.css("margin-bottom","30px");
		}else{
			usernameP.text("只支持中文字、英文字母、数字及_");
			$usernameP.css("display","block");
			$userDiv.css("margin-bottom","30px");
		}
	}
});
//密码
var $password = $("input[type=password]");
var $pswDiv = $("#pswDiv");
var $pswP = $("#pswP");
var pswflag = false;
$password.blur(function(){
	//每次进入函数都进行初始化为false
	pswflag = false;
	var value = $password.val();
	if(value.length>0 && check_password.test(value)){
		$pswP.text("");
		$pswP.css("display","none");
		$pswDiv.css("margin-bottom","20px");
		pswflag = true;
	}else{
		if(value.length<5){
			$pswP.text("最少需要输入 5 个字符");
			$pswP.css("display","block");
			$pswDiv.css("margin-bottom","30px");
		}else if(value.length>20){
			$pswP.text("最多只能输入 20 个字符");
			$pswP.css("display","block");
			$pswDiv.css("margin-bottom","30px");
		}
	}
});
//邮箱
var $email = $("input[name=email]");
var $emailP = $("#emailP");
var $emailDiv = $("#emailDiv");
var emailflag = false;
$email.blur(function(){
	//每次进入函数都进行初始化为false
	emailflag = false;
	var value = $email.val();
	if(value.length>0 && check_email.test(value)){
		var reslut = checkContain("email",value);
		console.log(reslut)
		if (reslut==1) {//数据库不存在，通过
			$emailP.text("");
			$emailP.css("display","block");
			$emailDiv.css("margin-bottom","20px");
			emailflag = true;
		}else{//不通过
			$emailP.text("邮箱号已存在!");
			$emailP.css("display","block");
			$emailDiv.css("margin-bottom","30px");
		}
	}else{
		$emailP.text("请输入有效邮箱号");
		$emailP.css("display","block");
		$emailDiv.css("margin-bottom","30px");
	}
});
//提交按钮事件
var $submit = $("input[type=submit]");
$submit.click(function(){
	var post = false;
	if($teleNum.val().length<=0){
		$teleP.text("请输入有效手机号");
		$teleP.css("display","block");
		$teleDiv.css("margin-bottom","30px");
	}
	if($username.val().length<=0){
		$usernameP.text("请输入用户名");
		$usernameP.css("display","block");
		$userDiv.css("margin-bottom","30px");
	}
	if($password.val().length<=0){
		$pswP.text("请输入密码");
		$pswP.css("display","block");
		$pswDiv.css("margin-bottom","30px");
	}
	if($email.val().length<=0){
		$emailP.text("请输入有效邮箱号");
		$emailP.css("display","block");
		$emailDiv.css("margin-bottom","30px");
	}
	if(teleflag && userflag && pswflag && emailflag){
		post = true;
	}
	return post;
});

function ajaxdopost(){

//	$.ajax({
//		  type: "POST",
//		  url: "/loginpage/ajax",
//		  data: { name: "John", location: "Boston" }
//		}).done(function( msg ) {
//		  alert( "Data Saved: " + msg );
//		});
}
function checkContain(checkType,value){
	var result;
	$.ajax({
		type:"post",
		async: false,
		data:{checkType:checkType ,value:value},
		url:"/loginpage/ajax",
		success:function(msg){
			result = msg;
		}
	});
	return result;
}